package com.ss.android.article.base.feature.video.cdn.cdnOptimize;

import android.text.TextUtils;

import com.bytedance.common.utility.aa;
import com.ss.android.article.base.feature.video.cdn.data.SSCdnIpItem;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy.Type;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;

public class SSCdnTachometerUtil {
    private static final int HTTP_REPONSE_LENGTH = 512;
    private static final int HTTP_TACGOMETER_TIMEOUT = 3000;

    public SSCdnTachometerUtil() {

    }

    public static void fetchVideoResponseData(SSCdnIpItem arg12) {
        HttpURLConnection v0_1 = null;
        if (arg12 != null && !TextUtils.isEmpty(arg12.ipAddress)) {
            int v8;
            for (v8 = 0; v8 < 2; ++v8) {
                long v10 = System.currentTimeMillis();
                HttpURLConnection v7 = null;
                try {
                    v0_1 = SSCdnTachometerUtil.openConnectionByIp(arg12.ipAddress, arg12.ipCdnUrl, 0, 512, 3000);
                    if (v0_1 != null) {
                        int v1 = 512;
                        if (SSCdnTachometerUtil.parseResponseData(((URLConnection) v0_1), v1)) {
                            arg12.isSuccess = true;
                            arg12.ipCostTime = System.currentTimeMillis() - v10;
                        }
                    }
                } catch (Throwable v0) {
                } finally {
                    if (v0_1 != null) {
                        try {
                            v0_1.disconnect();
                        } catch (Throwable v0) {
                        }
                    }
                }
            }
        }
    }

    private static HttpURLConnection openConnectionByIp(String arg8, String arg9, long arg10, long arg12, int arg14) throws IOException {
        HttpURLConnection v0_2;
        URLConnection v0_1;
        HttpURLConnection v1 = null;
        if ((TextUtils.isEmpty(((CharSequence) arg8))) || (TextUtils.isEmpty(((CharSequence) arg9)))) {
            v0_1 = v1;
        } else {
            String[] v3 = arg8.split("\\.");
            byte[] v4 = new byte[v3.length];
            int v5 = v3.length;
            int v0;
            for (v0 = 0; v0 < v5; ++v0) {
                v4[v0] = ((byte) Integer.parseInt(v3[v0], 10));
            }

            v0_1 = new URL(arg9).openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByAddress(v4), 80)));
            if (arg12 > 0) {
                ((HttpURLConnection) v0_1).setRequestProperty("Range", "bytes=" + arg10 + "-" + (arg10 + arg12));
            }

            if (arg14 > 0) {
                ((HttpURLConnection) v0_1).setConnectTimeout(arg14);
                ((HttpURLConnection) v0_1).setReadTimeout(arg14);
            }

            ((HttpURLConnection) v0_1).setUseCaches(false);
            ((HttpURLConnection) v0_1).setInstanceFollowRedirects(true);
            if (((HttpURLConnection) v0_1).getResponseCode() == 200) {
            } else {
                v0_1 = v1;
            }
        }

        return ((HttpURLConnection) v0_1);
    }

    private static boolean parseResponseData(URLConnection arg5, int arg6) {
        boolean v0 = false;
        try {
            String v1_1 = "";
            byte[] v2 = new byte[arg6];
            int v3 = arg5.getInputStream().read(v2, 0, arg6);
            if (v3 > 0) {
                v1_1 = aa.a(v2, v3);
            }
            if (TextUtils.isEmpty(((CharSequence) v1_1))) {
                return v0;
            }

            if (!v1_1.startsWith("00000020667479")) {
                return v0;
            }
        } catch (Throwable v1) {
            return v0;
        }

        return true;
    }
}

